"""
@File    :   datetime._convert.py
@Time    :   2024/02/22 15:43:11
@Author  :   Qrj
@Email   :   923207736@qq.com
@description   :   datetime工具类
"""

import time
from datetime import datetime
from typing import Literal


class DatetimeUtil:
    """datetime转换工具类"""

    @staticmethod
    def to_timestamp(
        dt: datetime,
        level: Literal["second", "millisecond", "microsecond"] = "millisecond",
        rounding: bool = False,
    ) -> int | float:
        """datetime转时间戳

        Args:
            dt (datetime): datetime对象
            level (Literal[&quot;second&quot;, &quot;millisecond&quot;, &quot;microsecond&quot;], optional): 秒级/毫秒级/微秒级
            round (bool, optional): 是否取整

        Returns:
            int | float: 时间戳
        """
        # 将datetime转为时间戳
        converted_timestamp = dt.timestamp()
        if level == "millisecond":
            # 毫秒级时间戳转换
            converted_timestamp = converted_timestamp * 1000
        elif level == "microsecond":
            # 微秒级时间戳转换
            converted_timestamp = converted_timestamp * 1000 * 1000
        if rounding:
            converted_timestamp = round(converted_timestamp)
        return converted_timestamp

    @staticmethod
    def get_diff_seconds_from_current_time(diff_dt: datetime) -> int:
        """获取传入的datetime与当前时间戳相差的秒数

        Args:
            diff_dt (datetime): 需要对比的datetime

        Returns:
            int: 相差的秒数
        """
        diff_dt_timestamp: int = DatetimeUtil.to_timestamp(
            diff_dt, level="second", rounding=True
        )
        now_timestamp: int = round(time.time())
        return diff_dt_timestamp - now_timestamp
